<!DOCTYPE HTML>
<html>
<head>
    {include file="common:meta"}
</head>
<body>
<div class="page-container">
    <div class="ibox float-e-margins">

        <div class="ibox-content">
            <div>
                <button class="all-enter  btn btn-warning  btn-sm">批量加入房间</button>
                <label class="control-label" style="margin-left: 50px">房主ID:</label>
                <label class="control-label">
                    <input type="number"  class="form-control" id="u_id" placeholder="房主ID必填">
                </label>
            </div>
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th><input id='checkAll' type="checkbox"></th>
                    <th>序号</th>
                    <th>用户ID</th>
                    <th>昵称</th>
                    <th>性别</th>
                    <th>头像</th>
                    <th>签名</th>
                    <th>生日</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {volist name="data" id="list" key="k"}
                <tr>
                    <td><input class='item' type="checkbox" value="{$list['id']}"></td>
                    <td>{$k}</td>
                    <td>{$list.u_id}</td>
                    <td>{$list.nickname}</td>
                    <td>{$list.sex}</td>
                    <td><img src="{$list.pic}" style="width: 10%;"/></td>
                    <td>{$list.signature}</td>
                    <td>{$list.birthday}</td>
                    <td><button class="enter-room btn btn-warning  btn-sm">进入房间</button></td>
                </tr>
                {/volist}
                </tbody>
            </table>
            {$data|raw}
        </div>
    </div>
</div>
{include file="common:footer"}
</body>
<script>


    $('.enter-room').click(function(){

        var u_id = $('#u_id').val();
        var user_id = $(this).parent().siblings().eq(0).children(0).val();

        console.log(user_id);
        console.log(u_id);
        if( u_id === '' ){
            layer.msg( '请填选房主ID',{icon:2,time:1000 });
            return false;
        }
        $.ajax({
            type: "get",
            url: "/RobotEnterRoom",
            data:{
                user_id:user_id,
                u_id:u_id
            } ,
            dataType: 'JSON',
            success: function (re) {
                if (re.code === 100) {
                    layer.msg(re.msg,{
                        icon:1,
                        time:1000
                    });
                    window.location.reload();
                } else {
                    layer.msg(re.msg,{
                        icon:2,
                        time:1000
                    });
                }
            },
            error: function () {
                layer.msg('服务器出小差了',{
                    icon:2,
                    time:1000
                });
            }
        });
    });

    var arr = [];
    $('#checkAll').click(function(){
        if($(this).prop('checked') === true){
            $('.item').prop('checked',true);
            $('.item:checked').each(function(){
                if(arr.indexOf($(this).val()) === -1)  //元素不存在为-1
                    arr.push($(this).val());
            });
        }else{
            $('.item').prop('checked',false);
            arr.splice(0,arr.length);
        }
        console.log(arr);
    });
    $('.item').click(function(){
        if($(this).prop('checked') === true){
            if(arr.indexOf($(this).val()) === -1)  //元素不存在为-1
                arr.push($(this).val());
        }else
            arr.splice(arr.indexOf($(this).val()),1);
        console.log(arr);
    });

    $('.all-enter').click(function(){


        var u_id = $('#u_id').val();
        if( u_id === '' ){
            layer.msg( '请填选房主ID',{icon:2,time:1000 });
            return false;
        }

        if(arr.length === 0)
            layer.msg('请勾选用户');
        else{
            layer.confirm('确认进入该房间吗',
                {
                    btn: ['确认', '取消'],
                    title: ['提示', 'font-size:18px;font-family:楷体;font-weight:700;']
                },function(){
                    $.ajax({
                        type:'get',
                        url:"/RobotAllEnterRoom?user_id=" +arr+'&u_id='+u_id,
                        async:true,
                        dataType:'json',
                        success:function(result){
                            if(result.code == 100){
                                layer.msg(result.msg,{
                                    icon:1,
                                    time:1000
                                });
                                window.location.reload();
                            }
                            else{
                                layer.msg(result.msg,{
                                    icon:2,
                                    time:1000
                                });
                            }
                        },
                        error:function(err){
                            layer.msg('服务器开小差了！',{
                                time:1500
                            });
                        }
                    });
                });
        }

    });
</script>
</html>